package cc.yihy.util.tree.bean;

import java.util.ArrayList;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Set;

/**
 * @author Yihy
 * @version V1.0
 * @Package cc.yihy.util.bean
 * @Description:
 * @date 2017/6/8 10:45
 */
public class TreeNode<K, V> {

    private K key;

    private V bean;

    /**
     * 父级
     */
    private TreeNode<K, V> parent;

    /**
     * 子级
     */
    private List<TreeNode<K, V>> children;


    public TreeNode(K key, V bean) {
        this.key = key;
        this.bean = bean;
        this.children = new ArrayList<>();
    }

    public K getKey() {
        return key;
    }

    public void setKey(K key) {
        this.key = key;
    }

    public V getBean() {
        return bean;
    }

    public void setBean(V bean) {
        this.bean = bean;
    }

    public TreeNode<K, V> getParent() {
        return parent;
    }

    public void setParent(TreeNode parent) {
        this.parent = parent;
    }

    public List<TreeNode<K, V>> getChildren() {
        return children;
    }

    public void setChildren(List<TreeNode<K, V>> children) {
        this.children = children;
    }


    public Set<V> getChildrenAll() {
        List<TreeNode<K, V>> children = getChildren();
        if (children == null || children.isEmpty()) return null;

        Set<V> result = new LinkedHashSet<>();
        for (TreeNode<K, V> child : children) {
            if (child.getBean() != null) result.add(child.getBean());
            Set<V> childrenAll = child.getChildrenAll();
            if (childrenAll != null) result.addAll(childrenAll);
        }
        return result;
    }


    @Override
    public String toString() {
        return "TreeNode{" +
                "key='" + key + '\'' +
                ", bean=" + bean +
                ", children=" + children +
                '}';
    }
}
